cmake_policy(SET CMP0048 NEW)
project(ap VERSION 0.1.0.0)

# lua version
if(IN_OPENWRT)
	set(LUA_INSTALL_DIR /usr/lib/lua)
	FIND_LIBRARY(LUA_MATH_LIBRARY m)
	FIND_LIBRARY(LUA_LIBRARY lua)
	SET( LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}" CACHE STRING "Lua Libraries")
else(IN_OPENWRT)
	include("../scripts/Findlua.cmake")
endif(IN_OPENWRT)

# include aux cmake scripts
include("../scripts/Gengetopt.cmake")
ADD_GENGETOPT_FILES(CMDLINE cmdline.ggo)

# lua install dir
set(LUA_INSTALL_APDIR ${LUA_INSTALL_DIR}/ap)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
	"${PROJECT_SOURCE_DIR}/config.h.in"
	"${PROJECT_BINARY_DIR}/config.h"
	)

include_directories("${CMAKE_CURRENT_BINARY_DIR}"
	"${PROJECT_BINARY_DIR}"
	"${PROJECT_SOURCE_DIR}/../include/")

set(ACLIST
apstatus.c
main.c
message.c
net.c
aplua.c
process.c
randomarr.c
)

set(LIBLIST
../lib/chap.c
../lib/arg.c
../lib/log.c
../lib/common.c
../lib/dllayer.c
../lib/md5.c
../lib/mjson.c
../lib/msg.c
../lib/netlayer.c
../lib/netdev.c
../lib/acctllua.c
../lib/sockarr.c
../lib/system.c
../lib/thread.c
)

# extra libs
find_package(Threads)
set (EXTRALIB ${LUA_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})

# target
add_executable(ap ${CMDLINE} ${ACLIST} ${LIBLIST} )
target_link_libraries (ap ${EXTRALIB})

# install
install(TARGETS ap DESTINATION bin)

# lua
file(GLOB LUA_PROG ./lua/*)
file(GLOB LUA_LIB ../lua/*)
install(FILES ${LUA_PROG} ${LUA_LIB} DESTINATION ${LUA_INSTALL_APDIR})
